package ttsolver.variables;

import ttsolver.enums.Direction;
import ttsolver.input.Node;
import ttsolver.input.Track;
import ttsolver.input.Train;

public class Xmove extends DefaultVariable {

	private Train train;
	private Track track;
	private int initTime;
	private int endTime;
	private double time;

	public Xmove() {
		super(VariableType.XMOVE);
	}

	public Xmove(Train t, Track track, int k, int l) {
		super(VariableType.XMOVE);
		this.train = t;
		this.track = track;
		this.initTime = k;
		this.endTime = l;
	}

	public Xmove(Train t, Track track) {
		super(VariableType.X);
		this.train = t;
		this.track = track;
	}

	public Train getTrain() {
		return train;
	}

	public void setTrain(Train train) {
		this.train = train;
	}
	
	public double getTime() {
		return time;
	}

	public void setTime(double time) {
		this.time = time;
	}

	public int getInitTime() {
		return initTime;
	}

	public void setInitTime(int initTime) {
		this.initTime = initTime;
	}

	public int getEndTime() {
		return endTime;
	}

	public void setEndTime(int endTime) {
		this.endTime = endTime;
	}

	public Track getTrack() {
		return track;
	}

	public void setTrack(Track track) {
		this.track = track;
	}

	public Node getInNode() {
		if (this.train.getDirection() == Direction.EASTBOUND)
			return track.getNodeOne();
		else
			return track.getNodeTwo();
	}

	public Node getOutNode() {
		if (this.train.getDirection() == Direction.EASTBOUND)
			return track.getNodeTwo();
		else
			return track.getNodeOne();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((train == null) ? 0 : train.getId().intValue());
		result = prime * result
				+ ((track == null) ? 0 : track.getNodeOne().getId().intValue());
		result = prime * result
				+ ((track == null) ? 0 : track.getNodeTwo().getId().intValue());
		result = prime * result + initTime;
		result = prime * result + endTime;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;

		Xmove other = (Xmove) obj;

		if (train == null) {
			if (other.train != null)
				return false;
		} else if (!train.equals(other.train)) {
			return false;
		}

		if (track == null) {
			if (other.track != null)
				return false;
		} else if (!track.equals(other.track)) {
			return false;
		}

		if (initTime != other.initTime) {
			return false;
		}

		if (endTime != other.endTime) {
			return false;
		}

		return true;
	}

	@Override
	public int compareTo(Object obj) {
		final int BEFORE = -1;
		final int EQUAL = 0;
		final int AFTER = 1;

		Xmove other = (Xmove) obj;

		if (this == other)
			return EQUAL;

		if (this.train != null && other.train != null) {
			if (this.train.getId() < other.train.getId())
				return BEFORE;
			if (this.train.getId() > other.train.getId())
				return AFTER;
		}

		if (this.getTrack().getNodeOne() != null
				&& other.getTrack().getNodeOne() != null) {
			if (this.getTrack().getNodeOne().getId() < other.getTrack()
					.getNodeOne().getId())
				return BEFORE;
			if (this.getTrack().getNodeOne().getId() > other.getTrack()
					.getNodeOne().getId())
				return AFTER;
		}

		if (this.getTrack().getNodeTwo() != null
				&& other.getTrack().getNodeTwo() != null) {
			if (this.getTrack().getNodeTwo().getId() < other.getTrack()
					.getNodeTwo().getId())
				return BEFORE;
			if (this.getTrack().getNodeTwo().getId() > other.getTrack()
					.getNodeTwo().getId())
				return AFTER;
		}

		if (this.initTime < other.initTime)
			return BEFORE;
		if (this.initTime > other.initTime)
			return AFTER;

		if (this.endTime < other.endTime)
			return BEFORE;
		if (this.endTime > other.endTime)
			return AFTER;

		return EQUAL;
	}

	@Override
	public String toString() {
		if (!printVar)
			return "";

		String s = (train != null) ? String.valueOf(train.getId()) : "";
		if (train.getDirection() == Direction.EASTBOUND) {
			s += (track.getNodeOne() != null) ? ","
					+ String.valueOf(track.getNodeOne().getId()) : "";
			s += (track.getNodeTwo() != null) ? ","
					+ String.valueOf(track.getNodeTwo().getId()) : "";
		} else {
			s += (track.getNodeTwo() != null) ? ","
					+ String.valueOf(track.getNodeTwo().getId()) : "";
			s += (track.getNodeOne() != null) ? ","
					+ String.valueOf(track.getNodeOne().getId()) : "";
		}
		if (type == VariableType.XMOVE) {
			s += "," + String.valueOf(initTime);
			s += "," + String.valueOf(endTime);
		}
		return "Xmove_(" + s + ")";
	}
}
